package com.java110.mall.shop.bmo.shop.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.account.AccountBondObjDto;
import com.java110.dto.distributionMode.DistributionModeDto;
import com.java110.dto.file.FileDto;
import com.java110.dto.shop.ShopDto;
import com.java110.intf.acct.IAccountBondObjDetailInnerServiceSMO;
import com.java110.intf.acct.IAccountBondObjInnerServiceSMO;
import com.java110.intf.system.IFileInnerServiceSMO;
import com.java110.intf.goods.IDistributionModeInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.po.account.AccountBondObjPo;
import com.java110.po.distributionMode.DistributionModePo;
import com.java110.po.shop.ShopPo;
import com.java110.mall.shop.bmo.shop.ISaveShopBMO;
import com.java110.utils.util.DateUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

@Service("saveShopBMOImpl")
public class SaveShopBMOImpl implements ISaveShopBMO {

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IFileInnerServiceSMO fileInnerServiceSMOImpl;

    @Autowired
    private IAccountBondObjInnerServiceSMO accountBondObjInnerServiceSMOImpl;

    @Autowired
    private IAccountBondObjDetailInnerServiceSMO accountBondObjDetailInnerServiceSMOImpl;
    @Autowired
    private IDistributionModeInnerServiceSMO distributionModeInnerServiceSMOImpl;

    /**
     * 添加小区信息
     *
     * @param shopPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> save(ShopPo shopPo, JSONObject accoutBond) {
        ShopDto shopDto = new ShopDto();
        shopDto.setShopName(shopPo.getShopName());
        int count = shopInnerServiceSMOImpl.queryShopsCount(shopDto);
        if (count > 0) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败，店铺名称重复！");
        }
        shopPo.setShopId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_shopId));
//        FileDto fileDto = new FileDto();
//        fileDto.setContext(shopPo.getShopLogo());
//        fileDto.setFileName(shopPo.getShopId());
//        String fileName = fileInnerServiceSMOImpl.saveFile(fileDto);
//        shopPo.setShopLogo(fileName);
        BigDecimal tmpMoney = new BigDecimal(accoutBond.getString("amount"));
        shopPo.setState(ShopDto.STATE_F);
        if (tmpMoney.compareTo(BigDecimal.ZERO) > 0) {
            shopPo.setState(ShopDto.STATE_B);
        }

        int flag = shopInnerServiceSMOImpl.saveShop(shopPo);
        if (flag < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
        }

        //默认保存保证金应收信息
        saveAccountBond(accoutBond, shopPo);

        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");


    }

    private void saveAccountBond(JSONObject accoutBond, ShopPo shopPo) {

        AccountBondObjPo accountBondObjPo = new AccountBondObjPo();
        accountBondObjPo.setBobjId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_bobjId));
        accountBondObjPo.setBondId(accoutBond.getString("bondId"));
        accountBondObjPo.setObjId(shopPo.getShopId());
        accountBondObjPo.setObjName(shopPo.getShopName());
        accountBondObjPo.setBondType(accoutBond.getString("bondType"));
        accountBondObjPo.setState("1001");
        if (ShopDto.STATE_F.equals(shopPo.getState())) {
            accountBondObjPo.setState(AccountBondObjDto.STATE_BUY_FINISH);
        }
        accountBondObjPo.setReceivableAmount(accoutBond.getString("amount"));

        accountBondObjPo.setStartTime(DateUtil.getNow("yyyy-MM-dd"));
        Calendar curr = Calendar.getInstance();
        curr.set(Calendar.MONTH, curr.get(Calendar.MONTH) + Integer.parseInt(accoutBond.getString("bondMonth")));
        Date date = curr.getTime();
        accountBondObjPo.setEndTime(DateUtil.getFormatTimeString(date, "yyyy-MM-dd"));
        accountBondObjPo.setReceivedAmount("0");
        accountBondObjPo.setRemark("系统自动生成保证金对象信息:" + accoutBond.getString("remark"));

        int flag = accountBondObjInnerServiceSMOImpl.saveAccountBondObj(accountBondObjPo);
        if (flag < 0) {
            throw new IllegalArgumentException("保存保证金对象失败");
        }

    }

}
